﻿using System;
using System.Globalization;
using System.Collections;
using System.Threading;

namespace Common.Util
{
    public class FDateTime
    {
        public static DateTime ResetTime(DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static int CompareDate(DateTime dt1, DateTime dt2)
        {
            dt1 = ResetTime(dt1);
            dt2 = ResetTime(dt2);
            return dt1.CompareTo(dt2);
        }

        public static int Days(DateTime dt1, DateTime dt2)
        {
            dt1 = ResetTime(dt1);
            dt2 = ResetTime(dt2);

            return dt2.Subtract(dt1).Days + 1;
        }

        public static string FormatDateTime(DateTime dt, string lang)
        {
            if (lang == "vi-VN")
            {
                return GetDayName(dt.DayOfWeek) + dt.ToString("dd/MM/yyyy - H:mm");
            }
            return dt.ToString("dddd, MMMM dd, yyyy - H:mm");
        }

        public static string FormatDate(DateTime dt, string lang)
        {
            if (lang == "vi-VN")
            {
                return GetDayName(dt.DayOfWeek) + dt.ToString("dd/MM/yyyy");
            }
            return dt.ToString("dddd, MMMM dd, yyyy");
        }

        /// <summary>
        /// Dinh dang ngay thang theo kieu "HH:mm:ss Ngày d tháng M năm yyyy"
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string FormatDateVneseGmt(DateTime dateTime)
        {
            return String.Format("{0:HH:mm:ss 'Ngày' d 'tháng' M 'năm' yyyy}", dateTime);
        }

        /// <summary>
        /// Dinh dang ngay thang theo kieu "HH:mm:ss Ngày d tháng M năm yyyy"
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string FormatDateVneseGmt(object dateTime)
        {
            return String.Format("{0:HH:mm:ss 'Ngày' d 'tháng' M 'năm' yyyy}", Convert.ToDateTime(dateTime));
        }

        public static string GetDayName(DayOfWeek dayofweek)
        {
            string st = "";
            switch (dayofweek)
            {
                case DayOfWeek.Monday:
                    st = "Thứ Hai ";
                    break;
                case DayOfWeek.Tuesday:
                    st = "Thứ Ba ";
                    break;
                case DayOfWeek.Wednesday:
                    st = "Thứ Tư ";
                    break;
                case DayOfWeek.Thursday:
                    st = "Thứ Năm ";
                    break;
                case DayOfWeek.Friday:
                    st = "Thứ Sáu ";
                    break;
                case DayOfWeek.Saturday:
                    st = "Thứ Bảy ";
                    break;
                case DayOfWeek.Sunday:
                    st = "Chủ Nhật ";
                    break;
            }
            return st;
        }

        public static string FormatDate(DateTime dt)
        {
            return FormatDate(dt, CultureInfo.CurrentCulture.Name);
        }

        public static string FormatDateTime(DateTime dt)
        {
            return FormatDateTime(dt, CultureInfo.CurrentCulture.Name);
        }

        #region Format Date
        /// <summary>
        /// Tra ve chuoi dinh dang theo format co dinh
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DefaultFormatDate(DateTime dt)
        {
            return dt.ToString("dd/MM/yyyy");
        }
        /// <summary>
        /// Tra ve chuoi dinh dang theo format co dinh
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DefaultFormatDate(string dt)
        {
            return DateTime.Parse(dt).ToString("dd/MM/yyyy");
        }

        /// <summary>
        /// Tra ve chuoi dinh dang theo format co dinh
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DefaultFormatDateTime(DateTime dt)
        {
            return dt.ToString("dd/MM/yyyy HH:mm:ss");
        }
        /// <summary>
        /// Tra ve chuoi dinh dang theo format co dinh
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DefaultFormatDateTime(string dt)
        {
            if (!IsDate(dt))
                return "";
            return DateTime.Parse(dt).ToString("dd/MM/yyyy HH:mm:ss");
        }

        /// <summary>
        /// Lay du lieu kieu ngay thang luu xuong DB
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DatabaseFormatDate(DateTime dt)
        {
            return dt.ToString("yyyy/MM/dd");
        }
        public static string FormatTime(int minutes, string lang)
        {
            int m = minutes % 60;
            int h = minutes - m;
            string strM = "minutes";
            string strH = "hours";
            if (lang == "vi-VN")
            {
                strM = "phút";
                strH = "giờ";
            }
            string time = "";

            if (h > 0)
            {
                time += (h / 60).ToString(CultureInfo.InvariantCulture) + " " + strH + " ";
            }

            if (m > 0)
            {
                time += m.ToString(CultureInfo.InvariantCulture) + " " + strM + " ";
            }

            return time;
        }

        public static string FormatTime(int minutes)
        {
            return FormatTime(minutes, CultureInfo.CurrentCulture.Name);
        }

        public static DateTime ConverDateFromString(string date)
        {
            string year = date.Substring(0, 4);
            string month = date.Substring(4, 2);
            string day = date.Substring(6, 2);
            string Date = day + "/" + month + "/" + year;
            return ConvertDate(Date);
        }

        public static DateTime ConvertDate(string value)
        {
            try
            {
                return DateTime.Parse(value, new CultureInfo("vi-VN"), DateTimeStyles.NoCurrentDateDefault);
            }
            catch
            {
                try
                {
                    return DateTime.Parse(value, new CultureInfo("en-US"), DateTimeStyles.NoCurrentDateDefault);
                }
                catch
                {
                    return DateTime.Now;
                }
            }
        }

        public static DateTime ConvertTime(string value)
        {
            try
            {
                string[] st = value.Trim().Split(':');
                int[] time = { int.Parse(st[0]), int.Parse(st[1]) };
                return new DateTime(1900, 1, 1, time[0], time[1], 0);
            }
            catch
            {
                return DateTime.Now;
            }
        }

        public static DateTime ConvertTime(DateTime date, TimeSpan time)
        {
            return new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds);
        }

        public static DateTime ConvertTime(DateTime date, string time)
        {
            string[] ts = time.Split(':');
            return new DateTime(date.Year, date.Month, date.Day, int.Parse(ts[0]), int.Parse(ts[1]), int.Parse(ts[2]));
        }

        public static string GetDate(object date)
        {
            if (date == null || date == DBNull.Value)
            {
                return "";
            }
            else
            {
                return DateTime.Parse(date.ToString()).ToString("dd/MM/yyyy HH:mm:ss");
            }
        }
        #endregion

        /// <summary>
        /// Kiem tra chuoi co phai la kieu ngay thang khong
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsDate(string date)
        {
            try
            {
                FNumber.ConvertDecimal(date);
                //double.Parse(date);
                return false;
            }
            catch (System.Exception e)
            {
                try
                {
                    DateTime.Parse(date, new CultureInfo("vi-VN"), DateTimeStyles.NoCurrentDateDefault);
                    return true;
                }
                catch
                {
                    try
                    {
                        DateTime.Parse(date, new CultureInfo("en-US"), DateTimeStyles.NoCurrentDateDefault);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                    return false;
                }
            }

        }

        #region Convert from string to DateTime

        /// <summary>
        /// Convert chuoi sang kieu DateTime
        /// </summary>
        /// <param name="input"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static bool ConvertToDateTime(string input, out DateTime dateTime)
        {
            return DateTime.TryParseExact(input, "M/d/yyyy", new CultureInfo("en-US"), DateTimeStyles.NoCurrentDateDefault,
                                   out dateTime);
        }
        #endregion
    }
}
